<?php

    class Page_Content extends ActiveRecord {
                
        function _setup()
        {
            $this->table = DB_PREFIX.'page_content';
        }
        
        function after_validation()
        {
            $content = $this->get('content');
            if(empty($content)) return;
            require VENDOR.'/htmlpurifier/HTMLPurifier.auto.php';
            $config = HTMLPurifier_Config::createDefault();
            $config->set('Core', 'Encoding', DEFAULT_CHARSET); //replace with your encoding
            $config->set('HTML','Doctype',HTML_DOCTYPE);
            $purifier = new HTMLPurifier($config);

            $clean_html = $purifier->purify($this->get('content'));
            
            // strip out html if it aint supposed to be there
            if ($this->get('section_type') != 'html') {
                $clean_html = strip_tags($clean_html);
            }
            
            $this->set('content',$clean_html);
        }
        
        // delete all content for given page
        function delete_page_content($page_id)
        {
            $this->db->Execute('DELETE FROM `'.$this->table.'` WHERE page_id=?',array($page_id)); 
        }
        
        // add to search index
        function after_validation_on_update()
        {
            $this->_update_search_index();          
        }
        // add to search index
        function after_validation_on_create()
        {                                        
            $this->_update_search_index();
        }
        
        function _update_search_index()
        {
            if(!$this->is_loaded()) return;
            // get the page that this belongs to, grab all of its content as one string,
            // then update the search_index_id
            use_model('pages');
            $pages = new Pages();
            $pages->load($this->get('page_id'));
            $pages->update_search_index_with_content();
        }
        ###                
        
        function find_by_section($section,$page_id)
        {
            $id = $this->find_rows(
                                array(
                                    'search'=>'first',
                                    'conditions'=>array('section=? and page_id=?',$section,$page_id),
                                    'fields'=>'id'
                                    )
                            );
            return $id['id'];
        }
    }

?>